package com.wrb.pwd.component;

import com.wrb.pwd.model.User;
import com.wrb.pwd.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

/**
 * @author wrb
 * @date 2019/10/8 14:48
 */
public class UserUpdateDialog extends JDialog {

	private int state;

	public int getReturnStatus() {
		return state;
	}

	public UserUpdateDialog(Component parentComponent, User user) {
		super((Frame) null, "编辑用户信息", true);

		// 设置对话框的宽高
		this.setSize(300, 340);
		// 设置对话框大小不可改变
		this.setResizable(false);
		// 设置对话框相对显示的位置
		this.setLocationRelativeTo(parentComponent);

		// 创建标签显示消息内容
		JLabel lbPlatform = new JLabel("平   台");
		JLabel lbName = new JLabel("用户名");
		JLabel lblPwd = new JLabel("密   码");
		JLabel lbPhone = new JLabel("手机号码");
		JLabel 	lbEmail = new JLabel("邮   箱");
		JLabel lbAuxEmail = new JLabel("辅助邮箱");
		JLabel lbRemark = new JLabel("备   注");

		//显示信息的标签
		JLabel lbIMsg = new JLabel();
		//设置标签的文字是红色
		lbIMsg.setForeground(Color.RED);

		JTextField txtPlatform = new JTextField(20);
		txtPlatform.setEditable(false);
		JTextField 	txtName = new JTextField(20);
		JTextField 	txtPwd = new JTextField(20);
		txtPwd.setEditable(false);
		JTextField 	txtPhone = new JTextField(20);
		JTextField 	txtEmail = new JTextField(20);
		JTextField 	txtAuxEmail = new JTextField(20);
		JTextField 	txtRemark = new JTextField(20);

		//设置初始值
		txtPlatform.setText(user.getPlatform());
		txtName.setText(user.getName());
		txtPwd.setText(user.getPassword());
		txtPhone.setText(user.getPhone());
		txtEmail.setText(user.getEmail());
		txtAuxEmail.setText(user.getAuxiliaryEmail());
		txtRemark.setText(user.getRemark());

		// 创建一个按钮用于关闭对话框
		JButton okBtn = new JButton("确定");
		JButton cancelBtn = new JButton("取消");
		okBtn.addActionListener(e -> {
			//设置信息标签为空 清楚原来的历史信息
			lbIMsg.setText("");
			//设置标签的文字是红色
			lbIMsg.setForeground(Color.RED);

			//获取平台
			String platformStr = txtPlatform.getText();
			if (platformStr == null || "".equals(platformStr.trim())) {
				lbIMsg.setText("平台不能为空");
				return;
			}
			//获取用户输入的用户名
			String nameStr = txtName.getText();
			if (nameStr == null || "".equals(nameStr.trim())) {
				lbIMsg.setText("用户名不能为空");
				return;
			}
			String pwdStr = txtPwd.getText();
			if (pwdStr == null || "".equals(pwdStr.trim())) {
				lbIMsg.setText("密码不能为空");
				return;
			}
			User updateUser = new User();
			updateUser.setId(user.getId());
//			updateUser.setPlatform(user.getPlatform());
			updateUser.setName(nameStr);
//			updateUser.setPassword(user.getPassword());
			String phoneStr = txtPhone.getText();
			if (phoneStr != null && !"".equals(phoneStr.trim())) {
				updateUser.setPhone(phoneStr);
			}
			String emailStr = txtEmail.getText();
			if (emailStr != null && !"".equals(emailStr.trim())) {
				updateUser.setEmail(emailStr);
			}
			String auxiliaryEmailStr = txtAuxEmail.getText();
			if (auxiliaryEmailStr != null && !"".equals(auxiliaryEmailStr.trim())) {
				updateUser.setAuxiliaryEmail(auxiliaryEmailStr);
			}
			String remarkStr = txtRemark.getText();
			if (remarkStr != null && !"".equals(remarkStr.trim())) {
				updateUser.setRemark(remarkStr);
			}
			Date now = new Date();
			user.setUpdateTime(now);

			boolean isSuccess = UserService.getInstance().updateUser(updateUser);
			if (isSuccess) {
				JOptionPane.showMessageDialog(this, "修改成功", "修改结果", JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(this, "程序出错了", "修改结果", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			this.state = 1;
			this.dispose();
		});
		cancelBtn.addActionListener(e -> this.dispose());

		lbPlatform.setBounds(20, 20, 60, 25);
		txtPlatform.setBounds(85, 20, 180, 25);
		lbName.setBounds(20, 50, 60, 25);
		txtName.setBounds(85, 50, 180, 25);
		lblPwd.setBounds(20, 80, 60, 25);
		txtPwd.setBounds(85, 80, 180, 25);
		lbPhone.setBounds(20, 110, 60, 25);
		txtPhone.setBounds(85, 110, 180, 25);
		lbEmail.setBounds(20, 140, 60, 25);
		txtEmail.setBounds(85, 140, 180, 25);
		lbAuxEmail.setBounds(20, 170, 60, 25);
		txtAuxEmail.setBounds(85, 170, 180, 25);
		lbRemark.setBounds(20, 200, 60, 25);
		txtRemark.setBounds(85, 200, 180, 25);
		lbIMsg.setBounds(60, 240, 180, 25);
		okBtn.setBounds(60, 270, 60, 25);
		cancelBtn.setBounds(130, 270, 60, 25);

		// 创建对话框的内容面板, 在面板内可以根据自己的需要添加任何组件并做任意是布局
		JPanel panel = new JPanel(null);

		// 添加组件到面板
		panel.add(lbPlatform);
		panel.add(txtPlatform);
		panel.add(lbName);
		panel.add(txtName);
		panel.add(lblPwd);
		panel.add(txtPwd);
		panel.add(lbPhone);
		panel.add(txtPhone);
		panel.add(lbEmail);
		panel.add(txtEmail);
		panel.add(lbAuxEmail);
		panel.add(txtAuxEmail);
		panel.add(lbRemark);
		panel.add(txtRemark);
		panel.add(lbIMsg);
		panel.add(okBtn);
		panel.add(cancelBtn);

		// 设置对话框的内容面板
		this.setContentPane(panel);
		//设置模式 dialog关闭后才能获取state的值
		this.setModal(true);
		// 显示对话框
		this.setVisible(true);
	}

}
